<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入搜索内容" v-model="query">
            <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="userDialogVisible = true">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="addUserForm" stripe border>
        <el-table-column type="index">
        </el-table-column>
        <el-table-column prop="username" label="姓名">
        </el-table-column>
        <el-table-column prop="email" label="邮箱">
        </el-table-column>
        <el-table-column prop="mobile" label="电话">
        </el-table-column>
        <el-table-column prop="role_name" label="角色">
        </el-table-column>
        <el-table-column prop="mg_state" label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.mg_state" @change="changeState(scope.row.id, scope.row.mg_state)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="190px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="editFrom(scope.row.id)"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeForm(scope.row.id)"></el-button>
            <el-tooltip class="item" effect="dark" content="分配角色" placement="top">
              <el-button type="warning" icon="el-icon-setting" size="mini" @click="setRoleForm(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="this.pagenum" :page-sizes="[2, 4, 6, 8]" :page-size="2" layout="total, sizes, prev, pager, next, jumper" :total="this.total">
      </el-pagination>
    </el-card>
    <!-- 弹出对话框-添加用户 -->
    <el-dialog title="提示" :visible.sync="userDialogVisible" width="50%" @close="resetForm">
      <!-- 表单验证 -->
      <el-form :model="userRuleForm" :rules="userRules" ref="userFormRef" label-width="70px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userRuleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userRuleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userRuleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="userRuleForm.mobile"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="userDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 弹出对话框-修改用户 -->
    <el-dialog title="提示" :visible.sync="editUserDialogVisible" width="50%" @close="editResetForm">
      <!-- 表单验证 -->
      <el-form :model="editUserRuleForm" :rules="editUserRules" ref="editUserFormRef" label-width="70px" class="demo-ruleForm">
        <el-form-item label="用户名">
          <el-input v-model="editUserRuleForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editUserRuleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="editUserRuleForm.mobile"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editResetForm">取 消</el-button>
        <el-button type="primary" @click="saveUser(editUserRuleForm.id)">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 弹出对话框-分配角色 -->
    <el-dialog title="提示" :visible.sync="roleDialogVisible" width="50%" @close="roleResetForm">
      <!-- 表单验证 -->
      <el-form label-width="70px">
        <p>当前的用户:{{userInfo.username}}</p>
        <p>当前的角色:{{userInfo.role_name}}</p>
        <p>分配新角色:
          <el-select v-model="selectedRole" placeholder="请选择" >
            <el-option v-for="(item,i) in roleOptions" :key="i" :label="item.roleName" :value="item.id">
            </el-option>
          </el-select>
        </p>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="roleResetForm">取 消</el-button>
        <el-button type="primary" @click="saveRole(userInfo.id)">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import mix from './User-mixins.js'
export default {
  data() {
    return {}
  },
  mixins: [mix]
}
</script>
<style lang="less" scoped>
</style>
